package com.bing.controller.LoanProducts;

import com.bing.entity.Statistical.ChannelAccessInfo;
import com.bing.entity.Statistical.ExpectRegistereInfo;
import com.bing.entity.Statistical.PartyStatisinfo;
import com.bing.entity.Statistical.ProductAccessInfo;
import com.bing.service.StatisticalService;
import com.bing.web.BaseController;
import com.bing.web.BaseResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: Mr.HRL
 * @Description: 统计
 * @Date: 2019-09-14 15:17
 * @Version: 1.0
 */
@RestController
@RequestMapping("/statist")
@Component
@Aspect
@CrossOrigin
public class StatisticalController extends BaseController {
    @Autowired
    StatisticalService statisticalService;

    @ApiOperation(value = "产品列表跳转详情-统计调用 -- app调用")
    @RequestMapping(value = "addProAccess")
    public ResponseEntity<BaseResponse> productAccessAdd(@RequestBody ProductAccessInfo productAccessInfo) {
        //统计点击次数
        statisticalService.sumClick(productAccessInfo);
        statisticalService.productAccessAdd(productAccessInfo);
        return buildSuccessResponse();
    }

    @ApiOperation(value = "预计注册-统计调用 -- app 调用")
    @RequestMapping(value = "addexceptReg")
    public ResponseEntity<BaseResponse> exceptRegAdd(@RequestBody ExpectRegistereInfo expectRegistereInfo) {
        //校验同一用户 产品ID不可重复
       // statisticalService.checkBpProdct(expectRegistereInfo);
        statisticalService.exceptRegAdd(expectRegistereInfo);
        return buildSuccessResponse();
    }



    @ApiOperation(value = "渠道访问-统计调用 -- PC调用")
    @RequestMapping(value = "addexcReg")
    public ResponseEntity<BaseResponse> channelAccessAdd(@RequestBody ChannelAccessInfo channelAccessInfo) {
        String eqId = null;
        try {
            eqId =  statisticalService.channelAccessAdd(channelAccessInfo);
        }catch (Exception e) {
            return buildFailResponse(e.getMessage());
        }
        return buildSuccessResponse(eqId);
    }




    @ApiOperation(value = "用户统计")
    @RequestMapping(value = "userStatis")
    public ResponseEntity<BaseResponse> userStatis(@RequestParam(required=false) String  startTime , @RequestParam(required=false) String endTime) throws ParseException {
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        Date date1 = null;
        if (StringUtils.isNotEmpty(startTime)) date =  format1.parse(startTime);
        if (StringUtils.isNotEmpty(endTime)) date1 = format1.parse(endTime);

        return buildSuccessResponse(statisticalService.userStatis(date,date1));
    }

    @ApiOperation(value = "产品统计")
    @RequestMapping(value = "productStatis")
    public ResponseEntity<BaseResponse> productStatis(@RequestParam(value = "startTime",required=false) String startTime ,
                                                      @RequestParam(value = "endTime",required=false)String endTime,
                                                      @RequestParam(value = "productName",required = false)String productName) throws ParseException {
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        Date date1 = null;
        if (StringUtils.isNotEmpty(startTime)) date  = format1.parse(startTime);
        if (StringUtils.isNotEmpty(endTime)) date1 =  format1.parse(endTime);

        return buildSuccessResponse(statisticalService.productStatis(date,date1,productName));
    }


    @ApiOperation(value = "渠道统计")
        @RequestMapping(value = "channelStatis")
    public ResponseEntity<BaseResponse> channelStatis(@RequestParam(value = "startTime",required=false) String startTime ,
                                                      @RequestParam(value = "endTime",required=false)String endTime,
                                                      @RequestParam(value = "channelName",required = false)String channelName) throws ParseException {
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        Date date1 = null;
        if (StringUtils.isNotEmpty(startTime)) date  = format1.parse(startTime);
        if (StringUtils.isNotEmpty(endTime)) date1 =  format1.parse(endTime);

        return buildSuccessResponse(statisticalService.channelStatis(date,date1,channelName));
    }


    @ApiOperation(value = "产品甲方数据上报")
    @RequestMapping(value = "addother")
    public ResponseEntity<BaseResponse> addPartyData(@RequestBody PartyStatisinfo partyStatisinfo){
        statisticalService.addPartyData(partyStatisinfo);
        return buildSuccessResponse();

    }

    @ApiOperation("导入甲方数据列表")
    @RequestMapping(value = "get")
    public ResponseEntity<BaseResponse> get(@RequestParam(value = "productName",required=false) String productName, @RequestParam(defaultValue = "1") Integer pageNum){
        return buildSuccessResponse(statisticalService.getPartyData(productName,pageNum));
    }
}
